﻿using Core.Persistence;
using Core.SystemLogs;
using Finbuckle.MultiTenant.Abstractions;
using Infrastructure.Tenant;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace Infrastructure.Persistence;

public sealed class LogDbContext : BoilerplateDbContext, ILogDbContext
{
    private readonly IOptions<DatabaseOptions> _settings;
    public LogDbContext(
        IMultiTenantContextAccessor<BoilerplateTenantInfo> multiTenantContextAccessor,
        DbContextOptions<LogDbContext> options,
        IPublisher publisher,
        IOptions<DatabaseOptions> settings)
        : base(multiTenantContextAccessor, options, publisher, settings)
    {
        _settings = settings;
    }

    public DbSet<SystemLog> SystemLogs => Set<SystemLog>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ArgumentNullException.ThrowIfNull(modelBuilder);
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(LogDbContext).Assembly);
        modelBuilder.ConfigureDateTimeForPostgreSQL(_settings.Value);
    }
}
